import type { ParsedAutoRouterOptions, ResolvedGlob } from '../types'
import { stat } from 'node:fs/promises'
import path from 'node:path'
import { globSync } from 'tinyglobby'
import { normalizePath } from 'unplugin-utils'
import { resolveImportPath } from '../shared'

export async function resolveGlobs(options: ParsedAutoRouterOptions): Promise<ResolvedGlob[]> {
  const { cwd, pageDir, pageInclude, pageExclude } = options
  const pageDirs = Array.isArray(pageDir) ? pageDir : [pageDir]

  const pageGlobs = pageDirs.flatMap((dir) => {
    const $pageDir = path.resolve(cwd, dir)

    const globs = globSync(pageInclude, {
      cwd: $pageDir,
      onlyFiles: true,
      ignore: pageExclude,
    })

    return globs.map(glob => resolveGlob(glob, dir, options))
  })

  const globs: ResolvedGlob[] = await Promise.all(
    pageGlobs.map(async (glob) => {
      const info = await stat(glob.filePath)

      return {
        ...glob,
        inode: info.ino,
      }
    }),
  )

  return globs
}

export function resolveGlob(glob: string, pageDir: string, options: Pick<ParsedAutoRouterOptions, 'cwd' | 'alias'>): ResolvedGlob {
  const { cwd, alias } = options

  const $pageDir = path.resolve(cwd, pageDir)

  const filePath = normalizePath(path.resolve($pageDir, glob))
  const importPath = resolveImportPath(filePath, alias)

  const resolvedGlob: Omit<ResolvedGlob, 'inode'> = {
    pageDir,
    glob,
    filePath,
    importPath,
  }

  return resolvedGlob as ResolvedGlob
}
